Copyright 2008 Joseph A. Papesh

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.




# import the appropriate python libraries - the python-twitter library must be updated with the change listed in Issue #22 to add support for since_id
# the url is provided here for your convenience http://code.google.com/p/python-twitter/issues/detail?id=22
import twitter
import math
import os
import smtplib
import time

from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders

# open a text file with the last id that was successfully pulled that is located in the same directory
fin = open('lastpulledid.txt')

# enter the gmail account information replace the <> and everything in between with the appropriate details
gmail_user = "<GMAILUSERID>"
gmail_pwd = "<GMAIL-PASSWORD>"

def mail(to, subject, text):
	msg = MIMEMultipart()

	msg['From'] = gmail_user
	msg['To'] = to
	msg['Subject'] = subject
	
	msg.attach(MIMEText(text))

	mailServer = smtplib.SMTP("smtp.gmail.com", 587)
   	mailServer.ehlo()
   	mailServer.starttls()
   	mailServer.ehlo()
   	mailServer.login(gmail_user, gmail_pwd)
   	mailServer.sendmail(gmail_user, to, msg.as_string())
   	# Should be mailServer.quit(), but that crashes...
   	mailServer.close()


# the python-twitter library requires Twitter Username and Password
api = twitter.Api(username='<TWITTER-USERNAME>', password='<TWITTER-PASSWORD>')

# read the last id from the file, and then close the file
last_id=fin.readline()
fin.close()

# initialize a couple of variables
message = ''
current_id = ''
i = 1

# setup an arbitrary loop (I have a cron job watching this process and restarts it if it quits).  The loop gets a friends timeline from Twitter with any updates utilizine the "since_id" attribute, and emails a  
# formatted email to the <DESTINATION-EMAIL>.  The loop then calls for a sleep of 90 seconds, and then repeats.
# the loop is designed to run indefinitely
while i > 0:
	statuses = api.GetFriendsTimeline(user='<TWITTER-USERNAME>', since_id=last_id)
	for s in statuses:
		message = message + s.user.name + '--' + s.text + '\n' + '\n'
	# the s.id value wasn't being handled properly, so now it verifies that the largest value is recorded as the last_id
		if s.id > int(last_id):
			last_id = s.id
	# open the lastpulledid.txt for writing, and overwrite the previous value
	fout = open('lastpulledid.txt','w')
	current_id = str(last_id) + '\n'
	fout.write(current_id)
	fout.close()
	if message !="":	
		mail("<DESTINATION-EMAIL>", "Twitter Timeline", message)
	time.sleep(90)
	message = ''


